package com.tjmc.tiomsresource.service.impl;

import com.tjmc.tiomscore.dao.TiDistrictCompanyMapper;
import com.tjmc.tiomscore.dao.TiDistrictMapper;
import com.tjmc.tiomscore.dao.TiSubnetMapper;
import com.tjmc.tiomscore.dao.TranPtnHwSbnMapper;
import com.tjmc.tiomscore.model.*;
import com.tjmc.tiomsresource.parser.CMSbnParser;
import com.tjmc.tiomsresource.service.CMBusiService;
import com.tjmc.tiomsresource.service.CMNelService;
import com.tjmc.tiomsresource.service.CMSbnService;
import com.tjmc.tiomsresource.utils.CommonUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Luc
 * @version 1.0
 * @date 2020-05-20 15:41
 */
@Service("cmSbnService")
public class CMSbnServiceImpl extends CMSuperServiceImpl<TranPtnHwSbn> implements CMSbnService {

    @Autowired
    private CMSbnParser cmSbnParser;
    @Autowired
    private TranPtnHwSbnMapper tranPtnHwSbnMapper;
    @Autowired
    private TiSubnetMapper tiSubnetMapper;
    @Autowired
    private TiDistrictMapper tiDistrictMapper;
    @Autowired
    private TiDistrictCompanyMapper tiDistrictCompanyMapper;
    @Autowired
    private CMNelService cmNelService;
    @Autowired
    private CMBusiService cmBusiService;

    @Override
    public CMSbnParser getParser() {
        return cmSbnParser;
    }

    @Override
    public TranPtnHwSbnMapper getMapper() {
        return tranPtnHwSbnMapper;
    }

    @Override
    public boolean saveTranPtnHwDataToDB(List<File> xmlFiles) {
        return this.saveTranPtnHwDataToDB(xmlFiles, "SBN");
    }

    @Override
    public List<TranPtnHwSbn> getAllTranPtnHwSbn() {
        return tranPtnHwSbnMapper.selectByExample(null);
    }

    @Override
    public void buildTiSubnetToDB() {
        tiSubnetMapper.truncateTable();
        tiSubnetMapper.insertDataFromTranPtnSbn();
        tiSubnetMapper.correctParentId();

        List<TiSubnet> tiSubnetList = tiSubnetMapper.selectByExample(null);
        if(null == tiSubnetList || tiSubnetList.size() == 0) {
            logger.info("查询 tiSubnetList为空");
            return;
        }
        List<TiDistrict> tiDistricts = tiDistrictMapper.selectWithCompanyByExample(null);
        Map<String, TiDistrict> districtMap = new HashMap<>();
        for(TiDistrict tiDistrict : tiDistricts) {
            if("和平".equals(tiDistrict.getNativename())) {
                districtMap.put("HP", tiDistrict);
            } else if("河西".equals(tiDistrict.getNativename())) {
                districtMap.put("HX", tiDistrict);
            } else if("南开".equals(tiDistrict.getNativename())) {
                districtMap.put("NK", tiDistrict);
            } else if("红桥".equals(tiDistrict.getNativename())) {
                districtMap.put("HQ", tiDistrict);
            } else if("河北".equals(tiDistrict.getNativename())) {
                districtMap.put("HB", tiDistrict);
            } else if("河东".equals(tiDistrict.getNativename())) {
                districtMap.put("HD", tiDistrict);
            } else if("东丽".equals(tiDistrict.getNativename())) {
                districtMap.put("DL", tiDistrict);
            } else if("西青".equals(tiDistrict.getNativename())) {
                districtMap.put("XQ", tiDistrict);
            } else if("津南".equals(tiDistrict.getNativename())) {
                districtMap.put("JN", tiDistrict);
            } else if("北辰".equals(tiDistrict.getNativename())) {
                districtMap.put("BC", tiDistrict);
            } else if("武清".equals(tiDistrict.getNativename())) {
                districtMap.put("WQ", tiDistrict);
            } else if("宝坻".equals(tiDistrict.getNativename())) {
                districtMap.put("BD", tiDistrict);
            } else if("塘沽".equals(tiDistrict.getNativename())) {
                districtMap.put("TG", tiDistrict);
            } else if("宁河".equals(tiDistrict.getNativename())) {
                districtMap.put("NH", tiDistrict);
            } else if("静海".equals(tiDistrict.getNativename())) {
                districtMap.put("JH", tiDistrict);
            } else if("蓟州".equals(tiDistrict.getNativename())) {
                districtMap.put("JX", tiDistrict);
            } else if("汉沽".equals(tiDistrict.getNativename())) {
                districtMap.put("HG", tiDistrict);
            } else if("大港".equals(tiDistrict.getNativename())) {
                districtMap.put("DG", tiDistrict);
            }
        }
        for(TiSubnet tiSubnet : tiSubnetList) {
            String nativename = tiSubnet.getNativename();
            if(StringUtils.isEmpty(nativename)) {
                continue;
            }
            String prefix = nativename.substring(0, 2);
            if(districtMap.keySet().contains(prefix)) {
                tiSubnet.setDistrictId(districtMap.get(prefix).getId());
                if(nativename.indexOf("AR") != -1 || nativename.indexOf("接入") != -1) {
                    tiSubnet.setLayer((byte) 3);
                } else if(CommonUtils.containsChinese(nativename)) {
                    tiSubnet.setLayer((byte) 3);
                } else {
                    tiSubnet.setLayer((byte) 2);
                }
//            } else if("核心汇聚层缩略图".equals(nativename)) {
//                tiSubnet.setLayer((byte) 2);
            } else if(nativename.indexOf("核心") != -1 && !"核心汇聚层缩略图".equals(nativename)) {
                tiSubnet.setLayer((byte) 1);
            }
            tiSubnetMapper.updateByPrimaryKeySelective(tiSubnet);
        }
    }

    @Override
    public TiDistrict getTiDistrictByTiSubnetId(Long tiSubnetId) {
        TiSubnet tiSubnet = tiSubnetMapper.selectByPrimaryKey(tiSubnetId);
        if(null != tiSubnet) {
            return tiDistrictMapper.selectByPrimaryKey(tiSubnet.getDistrictId());
        }
        return null;
    }

    @Override
    public TiDistrictCompany getTiDistrictCompanyByTiSubnetId(Long tiSubnetId) {
        TiDistrict tiDistrict = this.getTiDistrictByTiSubnetId(tiSubnetId);
        if(null != tiDistrict) {
            return tiDistrictCompanyMapper.selectByPrimaryKey(tiDistrict.getCompanyId());
        }
        return null;
    }

    @Override
    public List<TiDistrictCompany> getAllTiDistrictCompanies() {
        return tiDistrictCompanyMapper.selectByExample(null);
    }

    @Override
    public TiSubnet getTiSubnetByTiSubnetId(Long tiSubnetId) {
        return tiSubnetMapper.selectByPrimaryKey(tiSubnetId);
    }

    @Override
    public List<TiSubnet> getTiSubnetsByParentId(Long parentId) {
        TiSubnetExample tiSubnetExample = new TiSubnetExample();
        tiSubnetExample.createCriteria().andParentIdEqualTo(parentId);
        return tiSubnetMapper.selectByExample(tiSubnetExample);
    }

}